package com.cy.detectsys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.cy.detectsys.config.CustomException;
import com.cy.detectsys.config.SmallException;
import com.cy.detectsys.entity.*;
import com.cy.detectsys.mapper.*;
import com.cy.detectsys.model.OssFile;
import com.cy.detectsys.model.vo.RapiddnsVO;
import com.cy.detectsys.model.vo.WidePingVo;
import com.cy.detectsys.service.*;
import com.cy.detectsys.util.MinioTemplate;
import com.cy.detectsys.util.UUIDUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.cy.detectsys.constant.RSAKeyConstant.privateKey;
import static com.cy.detectsys.util.RSAUtils.*;

@Service
@Slf4j
public class DetectDataProcessServiceimpl implements DetectDataProcessService {
    @Resource
    private TcTaskMapper tcTaskMapper;

    @Resource
    private ErrLogService errLogService;

    @Override
    public void prepareData(String zipFilePath) {

        //根据路径分解出任务编号
        String taskNum = new File(zipFilePath).getName().replace(".zip", "");
        //根据任务编号
        Integer taskId = tcTaskMapper.selectTaskIdByTaskNum(taskNum);

        if (taskId == null) {
            throw new CustomException("探测任务不存在");
        }

        // 解压后文件夹路径
        String replace = zipFilePath.replace(".zip", "");

        File taskFolder = new File(replace);

        //获取解密信息
        HashMap aesMap = getAesMap(taskFolder);

        for (File probdeData : taskFolder.listFiles()) {

            if (probdeData.isDirectory()) {

                // 解析出 探针
                String probe = probdeData.getName().replace(String.valueOf(taskId), "");
                // 根据探针 去执行相应的数据解析任务

                processDetectData(probdeData, probe, aesMap);
            }
        }
    }


    @Resource
    private DbWikiMapper dbWikiMapper;

    @Override
    public void wiki(File file) {

        String content = FileUtil.readString(file, Charset.defaultCharset());
        String domain = file.getName().replaceAll(".json", "");
        DbWiki dbWiki = new DbWiki();
        dbWiki.setContent(content);
        dbWiki.setDomain(domain);
        try {
            dbWikiMapper.insert(dbWiki);
        } catch (DuplicateKeyException e) {
            //如果数据domain重复，直接更新
            dbWikiMapper.updateByDomian(dbWiki);
        }


    }

    @Resource
    private DbDnscrackMapper dnscrackMapper;

    @Override
    public void subDomainBrute(File file) {
        String messageDe = FileToString(file);
        //String filename = file.getName().substring(0, file.getName().lastIndexOf("."));
        if (StrUtil.isNotEmpty(messageDe)) {

            JSONObject dnscrackArray = (JSONObject) JSON.parse(messageDe);

            for (String domain : dnscrackArray.keySet()) {

                JSONObject subdomainlist = (JSONObject) dnscrackArray.get(domain);

                for (String subdomain : subdomainlist.keySet()) {

                    JSONArray ipList = (JSONArray) subdomainlist.get(subdomain);
                    DbDnscrack dnscrack = new DbDnscrack();
                    dnscrack.setKyEdDnscrackMainName(domain);
                    dnscrack.setKyEdDnscrackSubName(subdomain);
                    dnscrack.setKyEdDnscrackIpList(JSON.toJSONString(ipList));
                    dnscrackMapper.insert(dnscrack);

                }
            }
        }
    }


    @Resource
    private DbTargetSubdomainMapper targetSubdomainMapper;

    @Resource
    private DetectTargetMapper targetMapper;

    @Resource
    private DbDnsdumpsterMapper dnsdumpsterMapper;

    @Override
    public void dnsdumpster(File file) {
        String messageDe = FileToString(file);
        String filename = file.getName().substring(0, file.getName().lastIndexOf("."));

        DbDnsdumpster dnsdumpster = new DbDnsdumpster();
        dnsdumpster.setFilename(filename);
        if (StrUtil.isNotEmpty(messageDe)) {
            JSONObject domainMap = (JSONObject) JSON.parse(messageDe);
            for (String domainName : domainMap.keySet()) {
                JSONArray jsonArray = domainMap.getJSONArray(domainName);
                List<String> subdomainList = jsonArray.toJavaList(String.class);
                dnsdumpster.setKyEdDnsdumpsterSubdomain(JSON.toJSONString(subdomainList.stream().collect(Collectors.toSet())));
                if (StrUtil.isNotEmpty(filename)) {
                    //要根据域名Filename去查询targetid
                    DbTargetSubdomain targetSubdomain = new DbTargetSubdomain();
                    targetSubdomain.setTbNetTargetSubdomainType("117");
                    //根据domain查询targetid
                    String id = targetMapper.selectByDomainName(filename);
                    if (StrUtil.isNotEmpty(id)) {
                        targetSubdomain.setTbNetTargetId(id);
                        targetSubdomain.setTbNetTargetSubdomainDomain(domainName);

                    }
                    if (StrUtil.isNotEmpty(dnsdumpster.getKyEdDnsdumpsterSubdomain())) {
                        String kyEdDnsdumpsterSubdomain = dnsdumpster.getKyEdDnsdumpsterSubdomain();
                        try {
                            List<String> subdomains = JSON.parseArray(kyEdDnsdumpsterSubdomain, String.class);
                            for (String subdomain : subdomains) {
                                targetSubdomain.setTbNetTargetSubdomain(subdomain);
                                Set<String> origins = new HashSet<>();
                                origins.add("dnsdumpster");
                                targetSubdomain.setTbNetTargetSubdomainOrigin(JSON.toJSONString(origins.stream().collect(Collectors.toSet())));
                                targetSubdomain.setId(UUIDUtil.createUUID());
                                targetSubdomainMapper.insert(targetSubdomain);
                            }
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                    }
                }

            }
            dnsdumpsterMapper.insert(dnsdumpster);
        }
    }


    @Resource
    private DbAlienvaultService dbAlienvaultService;

    @Override
    public void alienvault(File file) {
        String messageDe = FileToString(file);
        try {
            messageDe = JSON.parse(messageDe).toString();
        } catch (Exception e) {
            log.error("json文件解析失败");
        }
        String filename = file.getName().substring(0, file.getName().lastIndexOf("."));
        if (StrUtil.isNotEmpty(messageDe)) {
            JSONObject parse = (JSONObject) JSON.parse(JSON.parse(messageDe).toString());
            JSONArray passive_dns = (JSONArray) parse.get("passive_dns");
            if (passive_dns != null) {
                List<DbAlienvault> alienvaults = JSON.parseArray(passive_dns.toJSONString(), DbAlienvault.class);

                for (DbAlienvault alienvault : alienvaults) {
                    alienvault.setKyEdAlienvaultFilename(filename);
                    //新增需求要求将ky_ed_alienvault_asn_name做拆分存成两个字段
                    if (StrUtil.isNotEmpty(alienvault.getKyEdAlienvaultAsnName())) {
                        String kyEdAlienvaultAsnName = alienvault.getKyEdAlienvaultAsnName();
                        String asnId = kyEdAlienvaultAsnName.substring(0, kyEdAlienvaultAsnName.indexOf(" "));
                        String asnName = kyEdAlienvaultAsnName.substring(kyEdAlienvaultAsnName.indexOf(" "));
                        alienvault.setKyEdAlienvaultAsnName(asnName);
                        alienvault.setKyEdAlienvaultAsnId(asnId);
                    }
                    if (StrUtil.isNotEmpty(alienvault.getKyEdAlienvaultFilename())) {
                        DbTargetSubdomain targetSubdomain = new DbTargetSubdomain();
                        targetSubdomain.setTbNetTargetSubdomainType("117");
                        //根据domain查询targetid
                        String id = targetMapper.selectByDomainName(filename);
                        if (StrUtil.isNotEmpty(id)) {
                            targetSubdomain.setTbNetTargetId(id);


                        }
                        targetSubdomain.setTbNetTargetSubdomainDomain(filename);

                        targetSubdomain.setTbNetTargetSubdomain(alienvault.getKyEdAlienvaultHostname());
                        Set<String> origins = new HashSet<>();
                        origins.add("alienvault");
                        targetSubdomain.setTbNetTargetSubdomainOrigin(JSON.toJSONString(origins.stream().collect(Collectors.toSet())));

                        targetSubdomain.setId(UUIDUtil.createUUID());

                        targetSubdomainMapper.insert(targetSubdomain);

                    }
                }
                dbAlienvaultService.saveBatch(alienvaults);
            }


        }
    }


    @Resource
    private DbSecuritytrailMapper securitytrailMapper;

    @Resource
    private DbTargetSubdomainService targetSubdomainService;

    @Override
    public void securitytrails(File file) {
        String messageDe = FileToString(file);
        String filename = file.getName().substring(0, file.getName().lastIndexOf("."));
        try {
            messageDe = JSON.parse(messageDe).toString();
        } catch (Exception e) {
            log.error("json文件解析失败");
        }
        if (StrUtil.isNotEmpty(messageDe) && !messageDe.equals("null") && !messageDe.contains("null")) {

            JSONArray objects = JSON.parseArray(messageDe);
            JSONObject parse = (JSONObject) objects.get(0);
            String subdomains = parse.getJSONArray("subdomains").toJSONString();
            List<String> strings = JSON.parseArray(subdomains, String.class);
            for (String string : strings) {
                String subdomain = string + "." + filename;
                DbSecuritytrail securitytrail = new DbSecuritytrail();
                securitytrail.setKyEdSecuritytrailsDomain(filename);
                securitytrail.setKyEdSecuritytrailsSubdomain(subdomain);
                securitytrailMapper.insert(securitytrail);
                /**
                 * 将所有子域名放入tb_net_target_subdomains表的ky_net_target_subdomain中，
                 * 如果已存在该子域名则需要在ky_net_target_subdomain_origin中增加来源
                 */
                try {
                    QueryWrapper<DbTargetSubdomain> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("tb_net_target_subdomain", subdomain);
                    DbTargetSubdomain one = targetSubdomainService.getOne(queryWrapper);
                    if (one == null) {
                        DbTargetSubdomain targetSubdomain = new DbTargetSubdomain();
                        targetSubdomain.setTbNetTargetSubdomainType("117");
                        Set<String> origins = new HashSet<>();
                        origins.add("securitytrails");
                        targetSubdomain.setTbNetTargetSubdomainOrigin(JSON.toJSONString(origins.stream().collect(Collectors.toSet())));
                        targetSubdomain.setId(UUIDUtil.createUUID());
                        targetSubdomain.setTbNetTargetSubdomainDomain(subdomain);
                        targetSubdomainMapper.insert(targetSubdomain);
                    } else {
                        one.setTbNetTargetSubdomainType("117");
                        String tbNetTargetSubdomainOrigin = one.getTbNetTargetSubdomainOrigin();
                        List<String> originlist = JSON.parseArray(tbNetTargetSubdomainOrigin, String.class);
                        originlist.add("securitytrails");
                        Set<String> collect = originlist.stream().collect(Collectors.toSet());
                        one.setTbNetTargetSubdomainOrigin(JSON.toJSONString(collect));
                        one.setTbNetTargetSubdomainDomain(filename);
                        targetSubdomainService.updateById(one);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        }
    }

    @Resource
    private DbRapiddnsMapper rapiddnsMapper;

    @Override
    public void rapiddns(File file) {
        String messageDe = FileToString(file);
        String filename = file.getName().substring(0, file.getName().lastIndexOf("."));
        try {
            messageDe = JSON.parse(messageDe).toString();
        } catch (Exception e) {
            log.error("json文件解析失败");
        }

        try {

            List<RapiddnsVO> rapiddns = new ArrayList<>();
            //List<JSONObject> rapiddnsorigin = ;
            JSONArray objects = JSONArray.parseArray(messageDe);
            for (Object o : objects) {
                JSONObject rapiddn = (JSONObject) o;
                RapiddnsVO rapiddnsVO = new RapiddnsVO();
                rapiddnsVO.setKyEdRapiddnsDomain(rapiddn.getString("Domain"));
                rapiddnsVO.setKyEdRapiddnsAddress(rapiddn.getString("Address"));
                rapiddnsVO.setKyEdRapiddnsType(rapiddn.getString("Type"));

                try {
                    String dateString = rapiddn.getString("Date");
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date parse = simpleDateFormat.parse(dateString);
                    rapiddnsVO.setKyEdRapiddnsDate(parse);
                    rapiddns.add(rapiddnsVO);
                } catch (ParseException e) {
                    System.out.println(rapiddn + "date异常");
                }

            }

            if (rapiddns != null) {
                for (RapiddnsVO rapiddnsVO : rapiddns) {
                    DbRapiddns rapiddn = new DbRapiddns();
                    BeanUtil.copyProperties(rapiddnsVO, rapiddn);
                    rapiddn.setFilename(filename);
                    if (StrUtil.isNotEmpty(filename)) {
                        //要根据域名Filename去查询targetid
                        DbTargetSubdomain targetSubdomain = new DbTargetSubdomain();
                        targetSubdomain.setTbNetTargetSubdomainType("117");
                        //根据domain查询targetid
                        String id = targetMapper.selectByDomainName(filename);
                        if (StrUtil.isNotEmpty(id)) {
                            targetSubdomain.setTbNetTargetId(id);

                        }
                        targetSubdomain.setTbNetTargetSubdomainDomain(filename);
                        targetSubdomain.setTbNetTargetSubdomain(rapiddn.getKyEdRapiddnsDomain());
                        Set<String> origins = new HashSet<>();
                        origins.add("rapiddns");
                        targetSubdomain.setTbNetTargetSubdomainOrigin(JSON.toJSONString(origins.stream().collect(Collectors.toSet())));
                        targetSubdomain.setId(UUIDUtil.createUUID());
                        DbTargetSubdomain existSubdomain = querySubdomainExist(targetSubdomain);
                        if (existSubdomain != null) {
                            String subdomainOrigin = existSubdomain.getTbNetTargetSubdomainOrigin();
                            List<String> originlist = JSON.parseArray(subdomainOrigin, String.class);
                            originlist.add("rapiddns");
                            existSubdomain.setTbNetTargetSubdomainDomain(filename);
                            existSubdomain.setTbNetTargetSubdomainOrigin(JSON.toJSONString(originlist.stream().collect(Collectors.toSet())));
                            try {
                                targetSubdomainMapper.updateById(existSubdomain);

                            } catch (Exception e) {
                                System.out.println(e);
                            }
                        } else {
                            try {
                                targetSubdomainMapper.insert(targetSubdomain);
                            } catch (Exception e) {
                                System.out.println(e);
                            }
                        }
                        //targetSubdomainMapper.insert(targetSubdomain);
                    }
                    rapiddnsMapper.insert(rapiddn);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void WildcardSubdomain(File file) {
        //subdomainMapper.updateSubdomainToNoClear();
        String name = file.getName();
        String text = FileToString(file);
        List<String> strings = JSON.parseArray(text, String.class);
        if (name.contains("effective")) {
            //如果是有效子域名将子域名id变化为121
            //for (String targetsubdomain : strings) {
            //    List<TargetSubdomain> tb_net_target_subdomain = targetSubdomainMapper.selectList(new QueryWrapper<TargetSubdomain>().eq("tb_net_target_subdomain", targetsubdomain));
            //    for (TargetSubdomain subdomain : tb_net_target_subdomain) {
            //        subdomain.setTbNetTargetSubdomainType("121");
            //        try {
            //            targetSubdomainMapper.updateById(subdomain);
            //        } catch (Exception e) {
            //            System.out.println(e);
            //        }
            //
            //    }
            //}
            //
            //QueryWrapper<TargetSubdomain> targetSubdomainQueryWrapper = new QueryWrapper<>();
            //targetSubdomainQueryWrapper.in("tb_net_target_subdomain", strings);


            UpdateWrapper<DbTargetSubdomain> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("tb_net_target_subdomain_type", 121);
            updateWrapper.in("tb_net_target_subdomain", strings);
            try {
                int updatenum = targetSubdomainMapper.update(null, updateWrapper);
                log.info("更新数量：" + updatenum);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        if (name.contains("invalid")) {
            for (String targetsubdomain : strings) {
                List<DbTargetSubdomain> tb_net_target_subdomain = targetSubdomainMapper.selectList(new QueryWrapper<DbTargetSubdomain>().eq("tb_net_target_subdomain", targetsubdomain));
                for (DbTargetSubdomain subdomain : tb_net_target_subdomain) {
                    targetSubdomainMapper.deleteById(subdomain);// 如果失效的话直接删除数据
                }
            }
        }


    }

    @Resource
    private DbWidePingService dbWidePingService;

    @Override
    public void wide_ping(File file) {
        String messageDe = FileToString(file);
        String filename = file.getName().substring(0, file.getName().lastIndexOf("."));

        try {
            messageDe = JSON.parse(messageDe).toString();
            List<WidePingVo> widePings = JSON.parseArray(messageDe, WidePingVo.class);
            List<DbWidePing> widePingList = new ArrayList<>();
            for (WidePingVo widePingVo : widePings) {
                DbWidePing widePing = new DbWidePing();
                BeanUtil.copyProperties(widePingVo, widePing);
                widePing.setKyEdWidePingSudomain(filename);
                widePingList.add(widePing);
            }
            if (widePingList.size() > 0) {
                updateSubdomainType(filename, 118);   //存活118  弃用 119
            } else {
                updateSubdomainType(filename, 119);
            }
            dbWidePingService.saveBatch(widePingList);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Resource
    private DbPortscanMapper portscanMapper;

    @Override
    public void portscan(File file) {
        String messageDe = FileToString(file);
        String filename = file.getName().substring(0, file.getName().lastIndexOf("."));
        try {
            if (StrUtil.isNotEmpty(messageDe)) {
                messageDe = JSON.parse(messageDe).toString();

                List<String> strings = JSON.parseArray(messageDe, String.class);
                for (String string : strings) {

                    String[] split = string.split(":");

                    DbPortscan portscan = new DbPortscan();
                    portscan.setKyEdPortscanPort(Integer.valueOf(split[1]));
                    portscan.setKyEdPortscanServer(split[0]);
                    portscan.setKyEdPortscanIp(filename);
                    portscanMapper.insert(portscan);
                }

            }
        } catch (Exception e) {
            throw new CustomException("解密后的json文件不是json格式");
        }
    }

    @Resource
    private DbCertificationdomainMapper certificationdomainMapper;

    @Override
    public void server_certification_download_tool(File file) {
        String messageDe = FileToString(file);
        String filename = file.getName().substring(0, file.getName().lastIndexOf("."));
        DbCertificationdomain certificationdomain = JSON.parseObject(String.valueOf(messageDe), DbCertificationdomain.class);
        try {
            certificationdomain.setKyEdCertificationdomainIp(filename.split("_")[0]);
            certificationdomain.setKyEdCertificationdomainPort(filename.split("_")[1]);
            certificationdomainMapper.insert(certificationdomain);
        } catch (Exception e) {
            System.out.println(e);
        }
    }


    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Override
    public void ipas(File file) {
        String messageDe = FileToString(file);//此时文件中的信息已经经过解密
        String ip = file.getName().replace(".json", "");
        if (StrUtil.isNotEmpty(messageDe)) {

            IndexRequest indexRequest = new IndexRequest();
            JSONObject jsonObject = JSON.parseObject(messageDe);
            if (!jsonObject.isEmpty()) {
                jsonObject.put("ip", ip);
                indexRequest.index("ipas").source(jsonObject, XContentType.JSON);
                try {
                    restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }

    @Resource
    private MinioTemplate minioTemplate;

    @Resource
    private DbTargetAssetsMapper dbTargetAssetsMapper;

    @Override
    public void ScanDomain(File file) {
        String subdomain = file.getName().split("_")[0];

        //去数据库里更新这个ip地址
//       将解密后的png或者html文件的路径　填入targetSubdomain表 还更新了当前数据的状态 从"存活"到"完成"
        if (file.getName().endsWith("html")) {

            OssFile ossFile = minioTemplate.upLoadFile("TCpackage", file);

            targetSubdomainMapper.updateHtmlPath(subdomain.replace(".html", ""), ossFile.getFilePath());

        }
        if (file.getName().endsWith("png")) {
            OssFile ossFile = minioTemplate.upLoadFile("TCpackage", file);

            targetSubdomainMapper.updatePngPath(subdomain.replace(".png", ""), ossFile.getFilePath());
        }

        String ipRegex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";

        if (subdomain.matches(ipRegex)) {
            DbTargetAssets targetAssets = new DbTargetAssets();
            targetAssets.setTbNetTargetAssetsIp(subdomain);
            if (file.getName().endsWith("html")) {
                targetAssets.setTbNetTargetAssetsHtml(file.getAbsolutePath());
            }
            if (file.getName().endsWith("png")) {
                targetAssets.setTbNetTargetAssetsPng(file.getAbsolutePath());
            }
            dbTargetAssetsMapper.insert(targetAssets);
        }

    }


    @Resource
    private DbFofaService fofaService;
    @Override
    public void fofa(File file) {
        String messageDe = FileToString(file);
        String filename = file.getName().substring(0, file.getName().lastIndexOf("."));
        JSONArray fofoArray = JSON.parseArray(messageDe);
        List<DbFofa> fofas = new ArrayList<>();
        for (Object fofaObject : fofoArray) {
            JSONArray fofoJSONArray = (JSONArray) fofaObject;
            DbFofa fofa = new DbFofa();
            for (int i = 0; i < 22; i++) {
                switch (i) {
                    case 0:
                        fofa.setKyEdFofaIp((String) fofoJSONArray.get(i));
                        break;
                    case 1:
                        fofa.setKyEdFofaPort((String) fofoJSONArray.get(i));
                        break;
                    case 2:
                        fofa.setKyEdFofaProtocol((String) fofoJSONArray.get(i));
                        break;
                    case 3:
                        fofa.setCity((String) fofoJSONArray.get(i));
                        break;
                    case 4:
                        fofa.setRegion((String) fofoJSONArray.get(i));
                        break;
                    case 5:
                        fofa.setCountry((String) fofoJSONArray.get(i));
                        break;
                    case 6:
                        fofa.setKyEdFofaDomain((String) fofoJSONArray.get(i));
                        break;
                    case 7:
                        fofa.setKyEdFofaIsp((String) fofoJSONArray.get(i));
                        break;
                    case 8:
                        fofa.setKyEdFofaTitle((String) fofoJSONArray.get(i));
                        break;
                    case 9:
                        fofa.setKyEdFofaHost((String) fofoJSONArray.get(i));
                        break;
                    case 10:
                        fofa.setKyEdFofaOs((String) fofoJSONArray.get(i));
                        break;
                    case 11:
                        fofa.setKyEdFofaHeader((String) fofoJSONArray.get(i));
                        break;
                    case 12:
                        fofa.setKyEdFofaBanner((String) fofoJSONArray.get(i));
                        break;
                    case 13:
                        fofa.setKyEdFofaCert((String) fofoJSONArray.get(i));
                        break;
                    case 14:
                        fofa.setKyEdFofaAsNumber((String) fofoJSONArray.get(i));
                        break;
                    case 15:
                        fofa.setKyEdFofaAsOrganization((String) fofoJSONArray.get(i));
                        break;

                    case 16:
                        fofa.setKyEdFofaJarm((String) fofoJSONArray.get(i));
                        break;

                    case 17:
                        fofa.setKyEdFofaCertsSubjectOrg((String) fofoJSONArray.get(i));
                        break;

                    case 18:
                        fofa.setKyEdFofaCertsSubjectCn((String) fofoJSONArray.get(i));
                        break;

                    case 19:
                        fofa.setKyEdFofaProduct2((String) fofoJSONArray.get(i));
                        break;

                    case 20:
                        fofa.setKyEdFofaProductCategory((String) fofoJSONArray.get(i));
                        break;

                    case 21:
                        fofa.setKyEdFofaVersion((String) fofoJSONArray.get(i));
                        break;

                    default:
                        break;
                }
            }
            //古月说文件名就是product
            fofa.setKyEdFofaProduct(filename);

            StringBuffer location = new StringBuffer();
            if (StrUtil.isNotEmpty(fofa.getCity())) {
                location.append(fofa.getCity());
            }
            if (StrUtil.isNotEmpty(fofa.getRegion())) {
                location.append("," + fofa.getRegion());
            }
            if (StrUtil.isNotEmpty(fofa.getCountry())) {
                location.append("," + fofa.getCountry());
            }
            fofa.setKyEdFofaLocation(location.toString());
            fofas.add(fofa);

            try {
                fofaService.save(fofa);
            } catch (Exception e) {
                System.out.println(e);

            }

        }

    }

    private void updateSubdomainType(String subdomainName, int status) {
        UpdateWrapper<DbTargetSubdomain> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("tb_net_target_subdomain", subdomainName);
        updateWrapper.set("tb_net_target_subdomain_type", status);
        targetSubdomainMapper.update(null, updateWrapper);
    }

    @Resource
    private DbCrtsubdomainService crtsubdomainService;

    @Override
    public void crtshD(File innerFile) {
        String filename = innerFile.getName().substring(0, innerFile.getName().lastIndexOf("."));

        String messageDe = FileToString(innerFile);
        try {
            messageDe = JSON.parse(messageDe).toString();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        if (StrUtil.isNotEmpty(messageDe)) {
            String substring = messageDe.substring(1, messageDe.length() - 1);
            try {
                List<DbCrtsubdomain> crtsubdomains = JSON.parseArray(substring, DbCrtsubdomain.class);
                for (DbCrtsubdomain crtsubdomain : crtsubdomains) {
                    if (StrUtil.isNotEmpty(crtsubdomain.getKyEdCrtsubdomainNameValue())) {
                        String kyEdCrtsubdomainNameValue = crtsubdomain.getKyEdCrtsubdomainNameValue();
                        String[] subdomians = kyEdCrtsubdomainNameValue.split("\\r?\\n");
                        for (String subdomian : subdomians) {
                            crtsubdomain.setKyEdCrtsubdomainNameValue(subdomian);
                            crtsubdomain.setKyEdCrtsubdomainDomainName(filename);
                            //crtsubdomain.getKyEdCrtsubdomainDomainName() 是域名
                            DbTargetSubdomain targetSubdomain = new DbTargetSubdomain();
                            targetSubdomain.setTbNetTargetSubdomainType("117");
                            if (StrUtil.isNotEmpty(crtsubdomain.getKyEdCrtsubdomainDomainName())) {
                                String id = targetMapper.selectByDomainName(filename);

                                if (StrUtil.isNotEmpty(crtsubdomain.getKyEdCrtsubdomainNameValue())) {
                                    targetSubdomain.setTbNetTargetSubdomain(crtsubdomain.getKyEdCrtsubdomainNameValue());
                                }
                                Set<String> origins = new HashSet<>();
                                origins.add("crtshD");
                                targetSubdomain.setTbNetTargetSubdomainOrigin(JSON.toJSONString(origins.stream().collect(Collectors.toSet())));
                                targetSubdomain.setId(UUIDUtil.createUUID());
                                DbTargetSubdomain existSubdomain = querySubdomainExist(targetSubdomain);
                                if (existSubdomain != null) {
                                    String subdomainOrigin = existSubdomain.getTbNetTargetSubdomainOrigin();
                                    List<String> originlist = JSON.parseArray(subdomainOrigin, String.class);
                                    originlist.add("crtshD");
                                    existSubdomain.setTbNetTargetSubdomainOrigin(JSON.toJSONString(originlist.stream().collect(Collectors.toSet())));
                                    targetSubdomainMapper.updateById(existSubdomain);
                                } else {
                                    targetSubdomainMapper.insert(targetSubdomain);
                                }
                                //targetSubdomainMapper.insert(targetSubdomain);
                            }
                        }
                    }
                }
                crtsubdomainService.saveBatch(crtsubdomains);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public DbTargetSubdomain querySubdomainExist(DbTargetSubdomain targetSubdomain) {
        if (targetSubdomain != null && StrUtil.isNotEmpty(targetSubdomain.getTbNetTargetSubdomain())) {
            QueryWrapper<DbTargetSubdomain> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tb_net_target_subdomain", targetSubdomain.getTbNetTargetSubdomain());
            try {
                DbTargetSubdomain one = targetSubdomainService.getOne(queryWrapper);
                return one;
            } catch (Exception e) {
                queryWrapper.last("limit 1");
                List<DbTargetSubdomain> list = targetSubdomainService.list(queryWrapper);
                return list.get(0);
            }

        } else {
            return null;
        }

    }

    @Resource
    private DetectDataProcessServiceimpl detectDataProcessService;

    private void processDetectData(File probdeDataFolder, String probe, HashMap aesMap) {

        Class<DetectDataProcessService> processService = DetectDataProcessService.class;

        Method doProbe = null;
        try {
            /**
             * 使用反射找到执行探针结果解析的方法
             *
             *  probe : 探针名称 对应 方法名称
             *  File.class :  对应方法的参数
             */
            doProbe = processService.getDeclaredMethod(probe, File.class);
        } catch (NoSuchMethodException e) {
            throw new CustomException(probe + "探针，服务端未提供探针结果解析的配置");
        }

        for (File probdeData : probdeDataFolder.listFiles()) {

            if (checkFileContent(probdeData, aesMap)) {

                try {
                    doProbe.invoke(detectDataProcessService, probdeData);
                } catch (Exception e) {
                    errLogService.small_err(probe + "探针解析文件功能异常" + e.getMessage() + "  文件名：" + probdeData.getName());
                }
            }


        }

    }

    private boolean checkFileContent(File probdeData, HashMap aesMap) {

//        if (probdeData.getName().endsWith(".png")) {
//            byte[] data = FileUtil.readBytes(probdeData.getAbsolutePath());
//            String outStr = Base64.encodeBase64String(data);
//            try {
//                privateKeyDecryptPNG(outStr, privateKey, probdeData.getAbsolutePath());
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//
//            return false;
//        }
//        else if (probdeData.getName().endsWith(".html")) {
//            byte[] data = FileUtil.readBytes(probdeData.getAbsolutePath());
//            String outStr = Base64.encodeBase64String(data);
//            String messageDe = null;
//            try {
//                messageDe = privateKeyDecrypt(outStr, privateKey);
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//            RewriteFile(probdeData, messageDe);
//
//            return false;
//        }else {

        if (probdeData.getName().endsWith(".png")) {
            return true;
        }


        byte[] aes_key = (byte[]) aesMap.get("aes_key");
        byte[] aes_iv = (byte[]) aesMap.get("aes_iv");

        byte[] data = FileUtil.readBytes(probdeData.getAbsolutePath());

        String content = aesDecrypt(data, aes_key, aes_iv);
        RewriteFile(probdeData, content);

        //String content = FileUtil.readString(probdeData, Charset.defaultCharset());

        //  手动异常处理
        content = content.replaceAll("\"\"", "");

        if (StrUtil.isEmpty(content)) {
            errLogService.small_err("数据内容为空");
            return false;
        }

//        }


        return true;
    }


}
